<?php
namespace app\index\model;

use think\Model;
use think\DB;

class UserWatchHistory extends Model
{
    public function getWatchHistory($uid){
        return DB::name('user_watch_history')->where('uid',$uid)->order('viewingtime desc')->select();
    }

    public function putWatchHistory($uid,$vid){
        //如果该用户的播放记录中有该视频，就更新时间，没有就增加一条播放记录
        $map = [
            'uid' => $uid,
            'vid' => $vid,
        ];
        $res = DB::name('user_watch_history')->where($map)->find();
        if(!empty($res)){
            $update_res = DB::name('user_watch_history')->where($map)->setField('viewingtime',time());
            return $update_res;
        }else{
            $data = [
                'uid' => $uid,
                'vid' => $vid,
                'viewingtime' => time(),
            ];
            $add_res = DB::name('user_watch_history')->insert($data);
            return $add_res;
        }
    }

}